package cz.zcu.fav.ratdroid.activity;

import cz.zcu.fav.ratdroid.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * Activity which is intended to load seach configuration and input from user.
 * Does not load anything from server.
 * 
 * @author Lukas Gemela
 * 
 */
public class SearchActivity extends Activity
{

	public static final int SEARCH_REQUEST_CODE = 2;
	public static String SEARCH_BY_KEY = "search.type";
	public static String SEARCH_STRING_KEY = "search.string";

	private RadioGroup radioGroup;
	private SearchType searchType;
	private EditText searchText;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_layout);
		searchText = (EditText) findViewById(R.id.search_text);
		radioGroup = (RadioGroup) findViewById(R.id.search_group);
		int selectedId = radioGroup.getCheckedRadioButtonId();
		searchType = evaluateSearchType(selectedId);
	}

	public void onSearchButtonClicked(View v)
	{
		if (searchText.getText().toString().isEmpty())
		{
			//no key specified
			Toast.makeText(getApplicationContext(),
					getResources().getString(R.string.searchEmptyTextfield),
					Toast.LENGTH_SHORT).show();
		} else
		{
			// we have everything what we need for search
			Intent returnIntent = new Intent();
			setResult(RESULT_OK, returnIntent);
			returnIntent.putExtra(SEARCH_BY_KEY, searchType);
			returnIntent.putExtra(SEARCH_STRING_KEY, searchText.getText()
					.toString());
			finish();
		}
	}

	public void onRadioButtonClicked(View v)
	{
		int selectedId = radioGroup.getCheckedRadioButtonId();
		searchType = evaluateSearchType(selectedId);
	}

	private SearchType evaluateSearchType(int selectedId)
	{

		switch (selectedId)
		{
		case R.id.search_all:
			return SearchType.ALL;
		case R.id.search_ident:
			return SearchType.OBJ_DESCRIPTION;
		case R.id.search_name:
			return SearchType.OBJ_NAME;
		case R.id.search_class_name:
			return SearchType.CLASS_NAME;
		default:
			return SearchType.ALL;
		}
	}

	public enum SearchType
	{
		ALL, CLASS_NAME, OBJ_NAME, OBJ_DESCRIPTION;
	}
}
